import { ref, computed } from 'vue';

export function useCategoryFiltering(categories) {
  // 响应式数据
  const selectedCategories = ref([]);
  const sortBy = ref('name');
  const viewMode = ref('grid');

  // 计算属性
  const totalProducts = computed(() => {
    return categories.reduce((total, category) => {
      return total + (category.products ? category.products.length : 0);
    }, 0);
  });

  // 过滤后的分类
  const filteredCategories = computed(() => {
    if (selectedCategories.value.length === 0) {
      return categories;
    }
    
    return categories.filter(category => 
      selectedCategories.value.includes(category.id)
    );
  });

  // 排序后的分类
  const sortedCategories = computed(() => {
    const categories = [...filteredCategories.value];
    
    switch (sortBy.value) {
      case 'name':
        return categories.sort((a, b) => a.name.localeCompare(b.name));
      case 'price':
        return categories.sort((a, b) => {
          const aMinPrice = getCategoryMinPrice(a);
          const bMinPrice = getCategoryMinPrice(b);
          return aMinPrice - bMinPrice;
        });
      case 'variants':
        return categories.sort((a, b) => {
          const aVariants = getCategoryTotalVariants(a);
          const bVariants = getCategoryTotalVariants(b);
          return bVariants - aVariants;
        });
      default:
        return categories;
    }
  });

  // 方法
  const toggleCategoryFilter = (categoryId) => {
    const index = selectedCategories.value.indexOf(categoryId);
    if (index > -1) {
      selectedCategories.value.splice(index, 1);
    } else {
      selectedCategories.value.push(categoryId);
    }
  };

  const removeCategoryFilter = (categoryId) => {
    const index = selectedCategories.value.indexOf(categoryId);
    if (index > -1) {
      selectedCategories.value.splice(index, 1);
    }
  };

  const clearAllFilters = () => {
    selectedCategories.value = [];
  };

  const getCategoryName = (categoryId) => {
    const category = categories.find(c => c.id === categoryId);
    return category ? category.name : '';
  };

  // 辅助方法
  const getCategoryMinPrice = (category) => {
    let minPrice = Infinity;
    if (category.products) {
      category.products.forEach(product => {
        if (product.variants) {
          product.variants.forEach(variant => {
            const price = parseFloat(variant.price);
            if (price < minPrice) {
              minPrice = price;
            }
          });
        }
      });
    }
    return minPrice === Infinity ? 0 : minPrice;
  };

  const getCategoryTotalVariants = (category) => {
    let totalVariants = 0;
    if (category.products) {
      category.products.forEach(product => {
        if (product.variants) {
          totalVariants += product.variants.length;
        }
      });
    }
    return totalVariants;
  };

  return {
    // 状态
    selectedCategories,
    sortBy,
    viewMode,
    
    // 计算属性
    totalProducts,
    filteredCategories,
    sortedCategories,
    
    // 方法
    toggleCategoryFilter,
    removeCategoryFilter,
    clearAllFilters,
    getCategoryName,
    getCategoryMinPrice,
    getCategoryTotalVariants
  };
}
